import { putBlockMode } from '@/api/block'
import { defineStore } from 'pinia'
import { ref } from 'vue'
import { useCarStore } from './car'
import { ElMessage } from './elMessage'

export const enum blockMode {
  VOID = '0',
  AVOID = '1',
  STOP = '2'
}

export const blockText = {
  [blockMode.VOID]: '非自主',
  [blockMode.AVOID]: '避障',
  [blockMode.STOP]: '停障'
}

export const useBlockStore = defineStore('block', () => {
  const carStore = useCarStore()
  const mode = ref<blockMode>(blockMode.VOID)

  async function changeBlockMode(val: blockMode) {
    mode.value = val
    await putBlockMode({ code: carStore.currentCar, mode: val })
    ElMessage({
      type: 'success',
      message: `切换为${blockText[val]}模式`
    })
  }

  return {
    mode,
    changeBlockMode
  }
})
